/**
 * Created by Administrator on 2016/11/8.
 */
class GameReadyScene extends ComponentEx {

    img_bg: eui.Image;
    img_title: eui.Image;
    lab_roomid: eui.Label;
    lab_rule: eui.Label;
    lab_description: eui.Label;

    btn_weixin: ButtonEx;
    btn_quit: ButtonEx;
    btnGroup: eui.Group;
    btn_chat: ButtonEx;
    btn_siri: ButtonEx;
    btn_renzheng: ButtonEx;
    btn_activity: ButtonEx;
    btn_invite: ButtonEx;
    btn_start: ButtonEx;
    btn_continue: ButtonEx;
    btn_hutishi: ButtonEx;

    gsView: GameScene;
    gsResultView: ResultScene;

    face: IGameTapEvent;


    gameFight: egret.Bitmap;
    gameFightTop: egret.Bitmap;
    //胡牌提示
    hcPanel: HandCardPromptPanel;

    constructor(face: IGameTapEvent) {
        super();
        this.skinName = 'GameReadySceneSkin';

        this.face = face;
        this.img_bg.touchEnabled = true;
    }

    childrenCreated() {
        super.childrenCreated();


        this.horizontalCenter = this.verticalCenter = 0;
        this.btn_hutishi.visible = false;

        this.top = this.bottom = this.left = this.right = 0;


        this.gsView = new GameScene(TouchBehaviour.i);
        this.addChild(this.gsView);

        this.gsResultView = new ResultScene;
        this.addChild(this.gsResultView);

        this.btn_hutishi.visible = false;
        /**this.btn_start.visible = false;
        this.btn_invite.visible = false;
        this.btn_continue.visible = false;
       

        this.addChild(this.gameFight = new egret.Bitmap(RES.getRes("game_fight")));
        this.addChild(this.gameFightTop = new egret.Bitmap(RES.getRes("game_fight")));
        this.gameFight.anchorOffsetX = this.gameFight.width >> 1;
        this.gameFight.anchorOffsetY = this.gameFight.height >> 1;
        this.gameFight.x = GSConfig.width >> 1;
        this.gameFight.y = GSConfig.height >> 1;

        this.gameFightTop.anchorOffsetX = this.gameFightTop.width >> 1;
        this.gameFightTop.anchorOffsetY = this.gameFightTop.height >> 1;
        this.gameFightTop.x = GSConfig.width >> 1;
        this.gameFightTop.y = GSConfig.height >> 1;*/

        this.gsView.visible = false;
        this.gsResultView.visible = false;

        this.btn_invite.visible = this.btn_start.visible = this.btn_continue.visible = false;

        this.hcPanel = new HandCardPromptPanel();
        this.hcPanel.visible = false;
        this.addChild(this.hcPanel);

        egret.Tween.get(this.btn_start, { loop: true }).to({ alpha: .5 }, 600).to({ alpha: 1 }, 600);

        this.updateConfig();
        this.updateDesktop();

        this.img_title.source = GameConfig.res_config['game_name_02'];

        this.img_bg.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.face.onBGTap, this.face);

        this.btn_weixin.addEventListener(egret.TouchEvent.TOUCH_TAP, this.face.onWeixinTap, this.face);
        this.btn_quit.addEventListener(egret.TouchEvent.TOUCH_TAP, this.face.onJiesanTap, this.face);
        this.btn_chat.addEventListener(egret.TouchEvent.TOUCH_TAP, this.face.onTalkTap, this.face);
        this.btn_renzheng.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
        this.btn_activity.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
        this.btn_invite.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
        this.btn_start.addEventListener(egret.TouchEvent.TOUCH_TAP, this.face.onStartGame, this.face);
        this.btn_continue.addEventListener(egret.TouchEvent.TOUCH_TAP, this.face.onRebackContinue, this.face);
        this.btn_hutishi.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);

        this.btn_siri.addEventListener(egret.TouchEvent.TOUCH_RELEASE_OUTSIDE, this.face.onSiriEnd, this);
        this.btn_siri.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.face.onSiriBegin, this);
        this.btn_siri.addEventListener(egret.TouchEvent.TOUCH_END, this.face.onSiriEnd, this);

        gameManager.i.addEventListener(GameEventType.TELPHONE_CONFIRM, this.updateConfig, this);
        gameManager.i.addEventListener(GameEventType.UPDATE_DESKTOP, this.updateDesktop, this);
        gameManager.i.addEventListener(GameEventType.UPDATE_ROOM_INFO, this.updateRoominfo, this);
    }

    private clickHandler(e: egret.TouchEvent) {
        if (!game.isBtnDisabledByTime) game.isBtnDisabledByTime = true;
        else return;
        switch (e.currentTarget) {
            case this.btn_renzheng:
                gameManager.open('TelphonePanel');
                break;
            case this.btn_activity:
                gameManager.open('NoticePanel');
                break;
            case this.btn_invite:
                GameConfig.initShare(game.isDaikai);
                game.share.show();
                break;
            case this.btn_hutishi:
                TouchBehaviour.i.onHutishiTap();
                break;
        }
    }

    //更新配置
    private updateConfig() {
        this.btn_renzheng.visible = !game.player.isVip && GameConfig.switch_shoujirenzheng;
        this.btn_activity.visible = game.player.isVip && GameConfig.switch_hongbao;
    }

    //更新背景颜色
    private updateDesktop() {
        this.img_bg.source = SettingPanel.getBg();
    }


    //更新房间信息
    private updateRoominfo() {
        this.lab_roomid.text = '房间号:' + game.roomid;
        this.lab_rule.text = game.ruleTexts();

    }
}